<?php
    error_reporting(E_ALL || ~E_DEPRECATED || ~E_STRICT || ~E_NOTICE || ~E_WARNING || ~E_CORE_WARNING);
    
//	$data=@$_POST["JsonString"];
	$dereceived=file_get_contents('php://input');
    file_put_contents("test.log","dereceived:".$dereceived." LINE: ".__LINE__."\n", FILE_APPEND);
//    $content = get_utf8_string($content);
//	$dom = DOMDocument::loadXML($content);

	$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 					//获取POST数据
	file_put_contents("test.log","postStr:".$postStr." LINE: ".__LINE__."\n", FILE_APPEND);
																//用SimpleXML解析POST过来的XML数据
	$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
	$fromUsername = $postObj->FromUserName; 					//获取发送方帐号（OpenID）
	$toUsername = $postObj->ToUserName; 						//获取接收方账号
	$keyword = trim($postObj->Content); 						//获取消息内容
	$masType = $postObj->MsgType;				//获取消息类型，可以作分类判断。本例默认是文本消息，不做判断
	$time = time(); 											//获取当前时间戳
    file_put_contents("test.log","fromUsername:".$fromUsername." toUsername:".$toUsername." keyword:".$keyword." masType:".$masType." time:".$time." LINE: ".__LINE__."\n", FILE_APPEND);
   	//---------- 返 回 数 据 ---------- //
  	//返回消息模板
   	$textTpl = "<xml>
    <ToUserName><![CDATA[%s]]></ToUserName>
    <FromUserName><![CDATA[%s]]></FromUserName>
    <CreateTime>%s</CreateTime>
    <MsgType><![CDATA[%s]]></MsgType>
    <Content><![CDATA[%s]]></Content>
    <FuncFlag>0</FuncFlag>
    </xml>";
     
    $msgType = "text"; //消息类型
    $contentStr = '您发送的是'.$keyword;
    $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
    file_put_contents("test.log","resultStr:".$resultStr." LINE: ".__LINE__."\n", FILE_APPEND);
    echo $resultStr;

    $signature=$_GET["signature"];
	$timestamp=$_GET["timestamp"];
	$nonce=$_GET["nonce"];
	$echostr=$_GET["echostr"];
	$token="dawu2019";
	file_put_contents("test.log","signature:".$signature." timestamp:".$timestamp." nonce:".$nonce." echostr:".$echostr." LINE: ".__LINE__."\n", FILE_APPEND);

	$signkey = array($token, $timestamp, $nonce);
	sort($signkey, SORT_STRING);
	$signString = implode($signkey);
	$signString = sha1($signString);
	file_put_contents("test.log","signString:".$signString." LINE: ".__LINE__."\n", FILE_APPEND);
	if($signString==$signature)
	{
		echo $echostr;
//		return;
	}

/*	private function transmitText($object, $content, $flag = 0)
	{
        $textTpl = "<xml>
	                    <ToUserName><![CDATA[%s]]></ToUserName>
	                    <FromUserName><![CDATA[%s]]></FromUserName>
	                    <CreateTime>%s</CreateTime>
	                    <MsgType><![CDATA[text]]></MsgType>
	                    <Content><![CDATA[%s]]></Content>
	                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }
	function get_utf8_string($content)                     //  将一些字符转化成utf8格式
	{    
	    $encoding = mb_detect_encoding($content, array('ASCII','UTF-8','GB2312','GBK','BIG5'));
	    return  mb_convert_encoding($content, 'utf-8', $encoding);
	}
 
	function get_xml_data($element, $tagname) 
	{
	    $tags = $element->getElementsByTagName($tagname);   //  取得所有的$tagname
	 
	    $tag = $tags->item(0);                              //  获取第一个以$tagname命名的标签
	    if ($tag->hasAttributes())                          //  获取data属性
	     {     
	        $attribute = $tag->getAttribute("data");
	        return $attribute;
	    }else 
	    {
	        return false;
	    }
	}
*/
//	echo "hello, this is handle view";

	exit(0);
?> 
